//
//  XJRequestHelper.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRequestHelper.h"
#import "XJNet.h"

static XJRequestHelper *helper = nil;

@implementation XJRequestHelper

/*! 初始化
 */
+ (instancetype)shareHelper{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[XJRequestHelper alloc]init];
    });
    return helper;
}


/*! 获取验证码
 */
- (void)
getCodeWithPhoneNumber:(NSString *)number type:(NSString *)type{
    
    NSDictionary * dic = @{@"phone":number,
                           @"type":type
                           };
    XJWeakSelf(self);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[XJNet loadNet] requestWithType:XJNetTypePost requestPath:HomePath dictionary:dic returnValueBlock:^(id returnValue) {
            NSDictionary * dataDic = returnValue[@"data"];
            XJLog(@"dataDic = %@",dataDic);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.returnBlock(dataDic);
            });
        } failureBlock:^{
            XJLog(@"fail");
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.failBlock();
            });
        } errorCodeBlock:^(id errorCode) {
            XJLog(@"errorCode = %@",errorCode);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.errorBlock(errorCode);
            });
        }];
    });
    
}

/*! 登录
 */
- (void)loginWithUserString:(NSString *)userStr
                       code:(NSString *)codeStr
                      style:(XJLoginStyle)loginStyle{
    
    
}

/*! 首页数据
 */
- (void)getHomeDataWithLatitude:(NSString *)lat
                      longitude:(NSString *)lng{
    NSDictionary * dic = @{@"lat":lat,
                           @"lng":lng
                           };
    XJWeakSelf(self);
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[XJNet loadNet] requestWithType:XJNetTypeGet requestPath:HomePath dictionary:dic returnValueBlock:^(id returnValue) {
            NSDictionary * dataDic = returnValue[@"data"];
            XJLog(@"dataDic = %@",dataDic);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.returnBlock(dataDic);
            });
        } failureBlock:^{
            XJLog(@"fail");
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.failBlock();
            });
        } errorCodeBlock:^(id errorCode) {
            XJLog(@"errorCode = %@",errorCode);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.errorBlock(errorCode);
            });
        }];
    });
}

@end
